package wqh.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.Device;
import wqh.global.GlobalDevice;
import wqh.pojo.IPConver;
import wqh.pojo.MapRet;
import wqh.service.DeviceService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by devts on 2016/12/8.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/tpson_api")
public class HkApi {

    @Autowired
    private DeviceService deviceService;

    @RequestMapping(value = "/water_pressure_host/getAll",method = RequestMethod.GET)
    public Object water_pressure_host_getAll()
    {
        try {
            List<Device> deviceList = deviceService.findAllRecord();
            if(deviceList.size() == 0) return MapRet.DATA(null);
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for(Device device : deviceList){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("host_name",device.getDevice_name());
                map.put("host_ip",device.getDevice_ip());
                map.put("host_port",device.getPort());
                map.put("host_description",device.getDetail());
                if(GlobalDevice.exist(device.getDevice_ip())){
                    map.put("host_online",1);
                }else{
                    map.put("host_online",0);
                }
                mapList.add(map);
            }
            return MapRet.DATA(mapList);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/water_pressure_data/getLastAll",method = RequestMethod.GET)
    public Object water_pressure_data_getAll()
    {
        try {
            return MapRet.DATA(GlobalData.getMap());
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/water_pressure_data/getLastOne",method = RequestMethod.GET)
    public Object water_pressure_data_one(@RequestParam(value = "ip",required = true) String ip)
    {
        try {
            if(!IPConver.isValidIp(ip)) return MapRet.IP_ERROR();
            if(GlobalData.getMap().size() == 0) return MapRet.DATA(null);
            Map<String,Object> map = new HashMap<String, Object>(GlobalData.getMap().get(0));
            for(Map.Entry<String,Object> m: map.entrySet()){
                if(m.getKey().equals(ip)){
                    Map<String,Object> dataMap = new HashMap<String, Object>();
                    dataMap.put("ip",m.getKey());
                    dataMap.put("channelData",m.getValue());
                    List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
                    mapList.add(dataMap);
                    return MapRet.DATA(mapList);
                }
            }
            return MapRet.DATA(null);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/water_pressure_data/cleanAll",method = RequestMethod.GET)
    public Object water_pressure_data_cleanAll()
    {
        try {
            GlobalData.clear();
            return MapRet.SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

}
